#include "stdafx.h"
#include <memory>
#include <xml/stream.h>
#define ICACHE_DLL __declspec(dllexport)
#include "icache/dll.h"
#include "icache/panel.h"
#include "icache/deinterlaced_footage.h"
#include "imglib/io.h"
#include "CacheSettingsDlg.h"
#include "ExtendedFileOpenDlg.h"

namespace icache {

xml::stream & operator || (xml::stream & s, footage_ptr_capsule & c)
{ 
	if (s.in()) {
		c.ptr = c.cache.create_footage(s, c.path);
	} else {
		c.ptr->serialize(s, c.path); 
	}
	return s;
}

struct dll::data
{
	std::auto_ptr<panel> panel;
	string open_file_filter;
	std::auto_ptr<CacheSettingsDlg> modaless_cfg;
};

dll::dll()
{
	_data = new data();
	_data->panel.reset(new panel());
}

dll::~dll()
{
	delete _data;
}

footage_sptr dll::create_avi_footage(const string & filename)
{
	return _data->panel->create_avi_footage(filename);
}

footage_sptr dll::create_seq_footage(const string & filename)
{
	return _data->panel->create_seq_footage(filename);
}

footage_sptr dll::create_still_footage(const string & filename)
{
	return _data->panel->create_still_footage(filename);
}

footage_sptr dll::create_footage(const string & filename)
{
	return _data->panel->create_footage(filename);
}

footage_sptr dll::create_footage(xml::stream & s, string const & reference_path)
{
	return _data->panel->create_footage(s, reference_path);
}

footage_sptr dll::create_footage_dialog(bool load_as_still_image, HWND hParent)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	string & f = _data->open_file_filter;
	if (f.empty()) {
		std::string img_formats = img::our_gdiplus_multireader().search_str();
		f = "All Supported Formats|*.avi;";
		f += img_formats;
		f += "|";
		f += "AVI Video (*.AVI)|*.AVI|";
		f += "Image Sequence (";
		f += img_formats;
		f += ")|";
		f += img_formats;
		f += "|All Files (*.*)|*.*||";
	}

	ExtendedFileOpenDlg dlg("avi", f.c_str(), load_as_still_image, CWnd::FromHandle(hParent));

	dlg.m_ofn.lpstrTitle = "Open Footage"; 
	if (dlg.DoModal() != IDOK) 
		return footage_sptr();

	if (dlg.m_bLoadAsStillImage)
		return create_still_footage((const char *)dlg.GetPathName());
	else 
		return deinterlaced_footage::create(
			dlg.m_DeinterlaceFields, dlg.m_DeinterlaceMethod,
			create_footage((const char *)dlg.GetPathName()));
}

cached_image_sptr dll::get_still_image(const string & filename)
{
	return _data->panel->get_still_image(filename);
}

cache & dll::coordinator()
{
	return _data->panel->coordinator();
}

void dll::configure(HWND hParent)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	CacheSettingsDlg dlg(CacheSettingsDlg::modal(), *_data->panel, CWnd::FromHandle(hParent));
	dlg.DoModal();
}

void dll::modaless_configure_show(HWND hParent)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	CWnd * pParent = CWnd::FromHandle(hParent);
	if (!_data->modaless_cfg.get())
		_data->modaless_cfg.reset(new CacheSettingsDlg(CacheSettingsDlg::modaless(), *_data->panel, pParent));
	else {
		if (_data->modaless_cfg->GetParent() != pParent)
			_data->modaless_cfg->SetParent(pParent);
	}
	_data->modaless_cfg->ShowWindow(SW_SHOW);
}

void dll::modaless_configure_hide()
{
	if (!_data->modaless_cfg.get())
		return;
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	_data->modaless_cfg->ShowWindow(SW_HIDE);
}

} //namespace icache
